home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d13 / ptv2n1.arc / VALSUB.ASM < prev    next >
Assembly Source File  |  1991-03-26  |  4KB  |  100 lines

  1.         PAGE    ,105
  2.         TITLE   Substitute VAL Functions
  3.         .MODEL  MEDIUM
  4. ;********************************************************************
  5. ;                              VALSUB.ASM                           *
  6. ;    Two assembled functions for compiled BASIC 7 programs to avoid *
  7. ; linking in the floating-point emulator code with VAL. Both return *
  8. ; the numeric equivalent of a string of decimal digits.             *
  9. ;    ATOI(<string>) [DECLARE FUNCTION ATOI% (A$)] returns the value *
  10. ; as an integer.  ATOL(<string>) [DECLARE FUNCTION ATOL& (A$)]      *
  11. ; returns the value as a long.                                      *
  12. ;    Both functions will delete leading spaces and will recognize a *
  13. ; leading minus sign.  Neither function recognizes hex or octal     *
  14. ; input.  WARNING:  Neither function is checked for overflow.       *
  15. ;-------------------------------------------------------------------*
  16. ;              Written by M. L. Lesser, January 20, 1991            *
  17. ;                 Assembled with Microsoft MASM v 5.1               *
  18. ;********************************************************************
  19.  
  20.         EXTRN StringAddress: FAR        ;BC 7 link-library "mixed
  21.         EXTRN StringLength:  FAR        ;   language" functioons
  22.  
  23. .CODE
  24.         PUBLIC  ATOI, ATOL
  25. ATOI    PROC                    ;The two procedures are identical -
  26. ATOL    PROC                    ;  the DECLARE statement sets return
  27.         PUSH    BP
  28.         MOV     BP,SP
  29.         PUSH    SI
  30.         PUSH    DI
  31.         PUSH    DS
  32.         XOR     AX,AX           ;Initialize MINUS flag (on stack)
  33.         PUSH    AX
  34. ; Find the string-space representation of the input string:
  35.         PUSH    6[BP]           ;Get length
  36.         CALL    StringLength    ;Returns length in AX
  37.         OR      AX,AX
  38.         JZ      NULL          ;Fix-up (at end) for null input string
  39.         PUSH    AX            ;Save length for later
  40.         PUSH    6[BP]         ;Get address of input string
  41.         CALL    StringAddress ;Returns far address in DX:AX
  42.         POP     CX            ;CX now has length of string
  43.         MOV     DS,DX         ;Set DS:DI to start of string
  44.         MOV     SI,AX
  45.         XOR     AX,AX           ;Clear critical registers
  46.         MOV     DX,AX
  47.         MOV     BX,AX
  48. DEBLANK:                        ;Delete any leading <SP> symbols
  49.         LODSB
  50.         CMP     AL,' '
  51.         JNZ     NEGIT
  52.         LOOP    DEBLANK
  53.         JCXZ    NULL            ;If string has all <SP> characters!
  54. NEGIT:  CMP     AL, '-'         ;Check for negative
  55.         JNZ     HERE            ;If not, check for valid digit
  56.         POP     BX              ;Else set MINUS flag
  57.         PUSH    AX
  58.         DEC     CX
  59. ; Evaluate digits in string, from left to right.  Value will be
  60. ;  accumulated in DX:BX, multiplying old value by ten before each
  61. ;  new digit is added in.  Quit at first non-digit byte.
  62. ADDEM:  LODSB
  63. HERE:   CMP     AL,'9'          ;Check if we are still within range
  64.         JA      NOMORE
  65.         CMP     AL,'0'
  66.         JB      NOMORE
  67.         AND     AL,0FH          ;Make into hex digit
  68.         SHL     BX,1            ;Multiply DX:BX by two
  69.         RCL     DX,1
  70.         MOV     BP,BX           ;Save twice value in DI:BP
  71.         MOV     DI,DX
  72.         SHL     BX,1            ;Now multiplied by four
  73.         RCL     DX,1
  74.         SHL     BX,1            ;By eight
  75.         RCL     DX,1
  76.         ADD     BX,BP           ;By ten
  77.         ADC     DX,DI
  78.         ADD     BX,AX           ;Plus new digit
  79.         ADC     DX,0
  80.         LOOP    ADDEM
  81. NOMORE: MOV     AX,BX           ;Positive value now in DX:AX
  82. ; Change sign of value if MINUS set:
  83.         POP     BX
  84.         OR      BX,BX
  85.         JZ      DONE            ;If not we are done
  86.         NEG     DX              ;Else negate DX:AX
  87.         NEG     AX
  88.         SBB     DX,0
  89. DONE:   POP     DS
  90.         POP     DI
  91.         POP     SI
  92.         POP     BP
  93.         RET     2
  94. NULL:   POP     AX              ;Fix up for no digits
  95.         MOV     DX,AX
  96.         JMP     DONE
  97. ATOL    ENDP
  98. ATOI    ENDP
  99.         END
  100.